package com.emapp.lccode.bean

import android.os.Build
import androidx.annotation.RequiresApi
import com.google.gson.GsonBuilder
import com.mylhyl.circledialog.callback.CircleItemLabel
import java.io.Serializable
import java.text.SimpleDateFormat
import java.time.Duration
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.Calendar
import java.util.Date

data class DeviceKt(
    var id: String = "",
    var num_code: String = "",
    var lng: String? = "0",
    var lat: String? = "0",
    var icon: String? = "",
    var title: String = "",
    var device_status: Int = 0,//0正常  1异常
    var name: String = ""
) : Serializable, CircleItemLabel {
    var question_sta : Int = 0// 设备状态 0正常  1丢失 2损坏 3其他
    override fun getItemLabel(): String {
        return name ?: ""
    }

    var data: ArrayList<DeviceKt> = arrayListOf()
    var operation_type = ""
    var work_type = ""
    var device_type = ""
    var area_city = ""
    var project_id = ""
    var wgs84_lng = ""
    var wgs84_lat = ""
    var upload_lnglat = ""
    var buildtime = ""
    var username = ""
    var phone = ""
    var create_time = ""
    var update_time = ""
    var delete_time = ""
    var province = ""
    var city = ""
    var district = ""
    var address = ""
    var file_ids = ""
    var materials_file_ids = ""
    var thumb = ""
    var boxcode = ""
    var imei = ""
    var build_company = ""
    var controller_company = ""
    var changjia = ""
    var flowmeter_num = ""
    var video_url = ""
    var year = ""
    var last_run_time = ""
    var run_num = ""
    var admin_id = 0
    var department_id = ""
    var toubiao_sta = ""
    var fid = ""
    var long_len = ""
    var depth = ""
    var width_len = ""
    var bianma_num = ""
    var rongliang = ""
    var check_type = ""
    var dn_norms = ""
    var water_outlet_plug = ""
    var remark = ""
    var projectname = ""
    var device_type_name = ""
    var work_type_name = ""
    var star_time = ""
    var jing_type = ""
    var xinghao = ""
    var keshu = ""
    var xiongjing = ""
    var scene_file_ids = ""
    var kuadu = ""
    var build_status = ""//1未开始,2进行中,3已完成
    var content = ""
    var date_time = ""//进度时间
    var welllname = ""//进度
    var department_name = ""//进度
    var device_id = ""
    var progress_id = ""
    var type_name = ""
    var certificate_num = ""
    var leimu = ""
    var bianpingui = ""
    var lixin = ""
    var diepian = ""
    var yalibiao = ""
    var sim = ""
    var iconthumb = ""
    var iconcolor = ""
    var iconstatus = ""
    var peitaojing=""
    var well_process_id=""//工序
    var json_text = ""
        get() {
            return if (field == null) "" else field
        }
    var fileArray: ArrayList<OrderFileKt> = arrayListOf()
    var materialsfileArray: ArrayList<OrderFileKt> = arrayListOf()
    var scene_file_arr: ArrayList<OrderFileKt> = arrayListOf()
    val lu: FeatureCollection?
        get() {
            return if (json_text.isNullOrBlank()) {
                null
            } else {
                val gson = GsonBuilder()
                    .registerTypeAdapter(Geometry::class.java, MapJsonDeserialize())
                    .create()
                gson.fromJson(json_text, FeatureCollection::class.java)
            }
        }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false
        return id == (other as DeviceKt).id
    }
    override fun hashCode(): Int {
        return id.hashCode()
    }
    fun daysSinceLastRun(): Int {
        if (last_run_time.isNullOrBlank()||last_run_time=="null"||last_run_time=="0")
            return 100
        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

        // 将字符串转换为Date对象
        val lastRunDate = dateFormat.parse(last_run_time) ?: return 100
        val currentDate = Date() // 获取当前日期时间
        // 将Date转换为Calendar对象
        val lastRunCalendar = Calendar.getInstance()
        lastRunCalendar.time = lastRunDate
        val currentCalendar = Calendar.getInstance()
        currentCalendar.time = currentDate
        // 计算天数差
        var daysDifference = (currentCalendar[Calendar.DAY_OF_YEAR] - lastRunCalendar[Calendar.DAY_OF_YEAR])

        // 考虑年份差异，如果当前日期已经过了去年的同一天，则需要加上一年的天数
        if (currentCalendar[Calendar.YEAR] > lastRunCalendar[Calendar.YEAR]) {
            val isLeapYear = { year: Int -> (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) }
            daysDifference += if (isLeapYear(lastRunCalendar[Calendar.YEAR])) 366 else 365
        }

        return daysDifference
    }

}
